package com.jctrip.hotel.source.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.jctrip.hotel.source.model.Result;
import com.jctrip.hotel.source.model.elong.req.offline.*;
import com.jctrip.hotel.source.model.elong.req.online.ElongHotelDetailReq;
import com.jctrip.hotel.source.model.elong.resp.offline.*;
import com.jctrip.hotel.source.model.elong.resp.online.ElongHotelDetailResp;
import com.jctrip.hotel.source.service.elong.ElongService;
import com.jctrip.hotel.source.service.elong.ElongTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author: zxy
 * @CreateTime: 2023-09-07 14:15
 * @Description:
 */
@Slf4j
@Api(tags = "elong")
@RestController
@RequestMapping("/elong")
@ApiSupport(order = 4)
public class ElongController {

    @Resource
    private ElongService elongService;

    @Autowired
    private ElongTaskService elongTaskService;

    @GetMapping("/queryElongCity")
    @ApiOperation(value = "获取城市数据", notes = "获取城市数据")
    @ApiOperationSupport(order = 1)
    public Result queryElongCity(@RequestParam String user,
                                 @RequestParam String appKey,
                                 @RequestParam String secretKey,
                                 @RequestParam(required = false) String local,
                                 @RequestParam(required = false) Integer countryType,
                                 @RequestParam(required = false) Integer cityIdType,
                                 @RequestParam(required = false) Boolean isNeedLoaction,
                                 @RequestParam(defaultValue = "10") Integer pageSize,
                                 @RequestParam(defaultValue = "1") Integer pageIndex) {
        ElongQueryCityResp result;
        try {
            result = elongService.queryElongCity(user, appKey, secretKey, local,
                    ElongQueryCityReq.builder()
                            .countryType(countryType)
                            .cityIdType(cityIdType)
                            .isNeedLocation(isNeedLoaction)
                            .pageIndex(pageIndex)
                            .pageSize(pageSize).build());
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @GetMapping("/queryElongHotelList")
    @ApiOperation(value = "获取酒店列表", notes = "获取酒店列表")
    @ApiOperationSupport(order = 2)
    public Result queryElongHotelList(@RequestParam String user,
                                      @RequestParam String appKey,
                                      @RequestParam String secretKey,
                                      @RequestParam(required = false) String local,
                                      @RequestParam(required = false) Date startTime,
                                      @RequestParam(required = false) Date endTime,
                                      @RequestParam String cityId,
                                      @RequestParam(defaultValue = "10") Integer pageSize,
                                      @RequestParam(defaultValue = "1") Integer pageIndex) {
        ElongQueryHotelListResp result;
        try {
            result = elongService.queryElongHotelList(user, appKey, secretKey, local,
                    ElongQueryHotelListReq.builder()
                            .startTime(startTime)
                            .endTime(endTime)
                            .cityId(cityId)
                            .pageIndex(pageIndex)
                            .pageSize(pageSize)
                            .build());
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @GetMapping("/queryElongHotel")
    @ApiOperation(value = "获取酒店详情", notes = "获取酒店详情")
    @ApiOperationSupport(order = 3)
    public Result queryElongHotel(@RequestParam String user,
                                  @RequestParam String appKey,
                                  @RequestParam String secretKey,
                                  @RequestParam(required = false) String local,
                                  @RequestParam String hotelId,
                                  @RequestParam(required = false) String options) {
        ElongQueryHotelResp result;
        try {
            result = elongService.queryElongHotel(user, appKey, secretKey, local,
                    ElongQueryHotelReq.builder()
                            .hotelId(hotelId)
                            .options(options)
                            .build());
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @GetMapping("/queryElongHotelRp")
    @ApiOperation(value = "产品详情", notes = "产品详情")
    @ApiOperationSupport(order = 4)
    public Result queryElongHotelRp(@RequestParam String user,
                                    @RequestParam String appKey,
                                    @RequestParam String secretKey,
                                    @RequestParam(required = false) String local,
                                    @RequestParam String hotelIds,
                                    @RequestParam(required = false) String paymentType,
                                    @RequestParam(required = false) String options,
                                    @RequestParam(required = false) String invoiceMode) {
        ElongQueryHotelRpResp result;
        try {
            result = elongService.queryElongHotelRp(user, appKey, secretKey, local,
                    ElongQueryHotelRpReq.builder()
                            .hotelIds(hotelIds)
                            .paymentType(paymentType)
                            .options(options)
                            .invoiceMode(invoiceMode)
                            .build());
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @PostMapping("/queryElongInventory")
    @ApiOperation(value = "房态库存", notes = "房态库存")
    @ApiOperationSupport(order = 5)
    public Result queryElongInventory(@RequestParam String user,
                                      @RequestParam String appKey,
                                      @RequestParam String secretKey,
                                      @RequestParam(required = false) String local,
                                      @RequestBody ElongQueryInventoryReq req) {
        ElongQueryInventoryResp result;
        try {
            result = elongService.queryElongInventory(user, appKey, secretKey, local, req);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @PostMapping("/queryElongRate")
    @ApiOperation(value = "产品价格", notes = "产品价格")
    @ApiOperationSupport(order = 6)
    public Result queryElongRate(@RequestParam String user,
                                 @RequestParam String appKey,
                                 @RequestParam String secretKey,
                                 @RequestParam(required = false) String local,
                                 @RequestBody ElongQueryRateReq req) {
        ELongQueryRateResp result;
        try {
            result = elongService.queryElongRate(user, appKey, secretKey, local, req);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @PostMapping("/validateElongOrder")
    @ApiOperation(value = "数据验证", notes = "数据验证")
    @ApiOperationSupport(order = 7)
    public Result validateElongOrder(@RequestParam String user,
                                     @RequestParam String appKey,
                                     @RequestParam String secretKey,
                                     @RequestParam(required = false) String local,
                                     @RequestBody ElongValidateReq req) {
        ElongValidateResp result;
        try {
            result = elongService.validateElongOrder(user, appKey, secretKey, local, req);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(result);
    }

    @PostMapping("/createOrderElongOrder")
    @ApiOperation(value = "创建订单", notes = "创建订单")
    @ApiOperationSupport(order = 8)
    public Result createElongOrder(@RequestParam String user,
                                   @RequestParam String appKey,
                                   @RequestParam String secretKey,
                                   @RequestParam(required = false) String local,
                                   @RequestBody ElongCreateOrderReq req) {
        ElongCreateOrderResp elongOrder;
        try {
            elongOrder = elongService.createElongOrder(user, appKey, secretKey, local, req);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(elongOrder);
    }

    @PostMapping("/queryOrderDetailResp")
    @ApiOperation(value = "查询订单")
    @ApiOperationSupport(order = 9)
    public Result queryOrderDetailResp(@RequestParam String user,
                                       @RequestParam String appKey,
                                       @RequestParam String secretKey,
                                       @RequestParam(required = false) String local,
                                       @RequestBody ElongQueryOrderDetailReq req) {
        ElongQueryOrderDetailResp orderDetailResp;
        try {
            orderDetailResp = elongService.queryElongOrderDetail(user, appKey, secretKey, local, req);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(orderDetailResp);
    }

    @PostMapping("/cancelOrder")
    @ApiOperation(value = "取消订单")
    @ApiOperationSupport(order = 10)
    public Result cancelorderResp(@RequestParam String user,
                                  @RequestParam String appKey,
                                  @RequestParam String secretKey,
                                  @RequestParam(required = false) String local,
                                  @RequestBody ElongCancelOrderReq req) {
        ElongCancelorderResp elongCancelorderResp;
        try {
            elongCancelorderResp = elongService.cancelElongOrder(user, appKey, secretKey, local, req);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(elongCancelorderResp);
    }

    @PostMapping("/getHotelDetailInfo")
    @ApiOperation(value = "实时接口:获取酒店详情信息")
    @ApiOperationSupport(order = 11)
    public Result getHotelDetailInfo(@RequestParam String user,
                                     @RequestParam String appKey,
                                     @RequestParam String secretKey,
                                     @RequestParam(required = false) String local,
                                     @RequestBody ElongHotelDetailReq req) {
        ElongHotelDetailResp elongHotelDetailResp;
        try {
            elongHotelDetailResp = elongService.queryElongHotelDetail(user, appKey, secretKey, local, req);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.ok(elongHotelDetailResp);
    }


    @GetMapping("/testPriceAndQuota")
    @ApiOperation(value = "testPriceAndQuota", notes = "价格落库")
    @ApiOperationSupport(order = 14)
    public void testPriceAndQuota(@RequestParam String sourceId,
                                  @RequestParam String user,
                                  @RequestParam String appKey,
                                  @RequestParam String secretKey,
                                  @RequestParam(required = false) String local,
                                  @RequestParam String hotel,
                                  @RequestParam String startDate,
                                  @RequestParam String endDate,
                                  @RequestParam Integer days) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            elongTaskService.syncHotelRates(sourceId, user, appKey, secretKey, local,
                    hotel, simpleDateFormat.parse(startDate), simpleDateFormat.parse(endDate), days);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    @GetMapping("/testXxx")
    @ApiOperation(value = "测选择")
    @ApiOperationSupport(order = 15)
    public void testXxx(@RequestParam String hotelId,
                        @RequestParam String sourceId) {
        elongTaskService.syncRatesChose(sourceId, hotelId);
    }
}
